package com.gusto.engine.clusterant.access.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.gusto.engine.clusterant.access.PlanDAO;
import com.gusto.engine.clusterant.model.Point;

/**
 * <p>Database {@link PlanDAO} implementation</p>
 * 
 * @author amokrane.belloui@gmail.com
 *
 */
public class PlanDAOImpl implements PlanDAO {
	
	String TABLE_PLAN = "clusterant_plan";
	String COLUMN_COL = "col";
	String COLUMN_ROW = "row";
	String COLUMN_OBJECT = "object";
	String COLUMN_PLAN_ID = "plan_id";
	
	private int CONTEXT;
	public void setCONTEXT(int context) {
		CONTEXT = context;
	}
	
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	 
	public PlanDAOImpl() {
		super();
	}
	
	@SuppressWarnings("unchecked")
	public List<Point> loadPoints() {
		String sql = "select * from " + TABLE_PLAN + " where " + COLUMN_PLAN_ID + " = ? ";
		Object[] params = new Object[] { CONTEXT };
		int[] types = new int[] { Types.INTEGER };
		List<Point> result = jdbcTemplate.query(sql, params, types, new PointRowMapper());
		return result;
	}
	
	public void removeObject(int row, int col) {
		String remove = 
			"delete from " + TABLE_PLAN + " " + 
			"where " + COLUMN_PLAN_ID + " = ? " +
			"and " + COLUMN_COL + " = ? " +
			"and " + COLUMN_ROW + " = ? ";
		
		Object[] params = new Object[] { CONTEXT, col, row };
		int[] types = new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER };
		
		jdbcTemplate.update(remove, params, types);
	}
	
	public void removeAllObjects() {
		String remove = 
			"delete from " + TABLE_PLAN + " " + 
			"where " + COLUMN_PLAN_ID + " = ? ";
		
		Object[] params = new Object[] { CONTEXT };
		int[] types = new int[] { Types.INTEGER };
		
		jdbcTemplate.update(remove, params, types);
	}
	
	public void putObject(int row, int col, Object object) {
		String insert = 
			"insert into " + TABLE_PLAN + " " + 
			"(" + COLUMN_PLAN_ID + ", " + COLUMN_COL + ", " + COLUMN_ROW + ", " + COLUMN_OBJECT + ") " +
			"values (?, ?, ?, ?)";
		
		// FIXME Long to String
		Object[] params = new Object[] { CONTEXT, col, row, object };
		int[] types = new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR };
		
		jdbcTemplate.update(insert, params, types);
	}

	public String getTABLE_PLAN() {
		return TABLE_PLAN;
	}

	public void setTABLE_PLAN(String table_plan) {
		TABLE_PLAN = table_plan;
	}

	public String getCOLUMN_COL() {
		return COLUMN_COL;
	}

	public void setCOLUMN_COL(String column_col) {
		COLUMN_COL = column_col;
	}

	public String getCOLUMN_ROW() {
		return COLUMN_ROW;
	}

	public void setCOLUMN_ROW(String column_row) {
		COLUMN_ROW = column_row;
	}

	public String getCOLUMN_OBJECT() {
		return COLUMN_OBJECT;
	}

	public void setCOLUMN_OBJECT(String column_object) {
		COLUMN_OBJECT = column_object;
	}

	public String getCOLUMN_PLAN_ID() {
		return COLUMN_PLAN_ID;
	}

	public void setCOLUMN_PLAN_ID(String column_plan_id) {
		COLUMN_PLAN_ID = column_plan_id;
	}
	
}

class PointRowMapper implements RowMapper {
	
	public PointRowMapper() {
	}
	
	public Object mapRow(ResultSet rs, int index) throws SQLException {
		Point point = new Point(rs.getInt("row"), rs.getInt("col"));
		// FIXME Long to String
		point.setObject(rs.getString("object"));
	    return point;
	}
	
}

